<?php
	include_once('dbcon.php');
	include_once('constants.php');
	include_once('error_log_db.php');
	include_once('general_functions_db.php');
	
	define("NULL_SCORE_VALUE", -9999);

	function get_user_category_db($limit, $from_id, $user_id) {
		global $mysqli;

		if ( $from_id != -1 ) {
			$category_query = $mysqli->query("SELECT * FROM categories WHERE category_id<".$from_id." && user_id=".$user_id." ORDER BY category_id DESC LIMIT ".$limit);
		} else {
			$category_query = $mysqli->query("SELECT * FROM categories WHERE user_id=".$user_id." ORDER BY category_id DESC LIMIT ".$limit);
		}

		logIfError($mysqli, $user_id, "get_categories_db", "get_user_category_db", "1");

		return return_category($category_query, $user_id);
	}

	function get_best_category($user_id) {
		global $mysqli;
		
		$category_query = $mysqli->query("SELECT * FROM categories ORDER BY score DESC LIMIT 1");
		logIfError($mysqli, $user_id, "get_categories_db", "get_best_category", "1");
		
		$category = buildCategoryArray($category_query->fetch_assoc(), $user_id);

		return json_encode($category, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	function get_one_category($category_id, $user_id) {
		global $mysqli;
		
		$category_query = $mysqli->query("SELECT * FROM categories WHERE category_id=".$category_id);
		logIfError($mysqli, $user_id, "get_categories_db", "get_one_category", "1");
		
		$category = buildCategoryArray($category_query->fetch_assoc(), $user_id);

		return json_encode($category, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	function get_bests_lasts_categories_db($limit, $from_id, $from_score, $user_id, $get_best) {
		global $mysqli;

		if ($get_best == 1) {
			if ( $from_score != NULL_SCORE_VALUE )
				$category_query = $mysqli->query("SELECT * FROM categories WHERE score<".$from_score." ORDER BY score DESC LIMIT ".$limit);
			else
				$category_query = $mysqli->query("SELECT * FROM categories ORDER BY score DESC LIMIT ".$limit);
		} else {
			if ( $from_id != -1 ) {
				$category_query = $mysqli->query("SELECT * FROM categories WHERE category_id<".$from_id." ORDER BY category_id DESC LIMIT ".$limit);
			} else {
				$category_query = $mysqli->query("SELECT * FROM categories ORDER BY category_id DESC LIMIT ".$limit);
			}
		}

		logIfError($mysqli, $user_id, "get_categories_db", "get_bests_lasts_categories_db", "1");

		return return_category($category_query, $user_id);
	}

	function return_category($category_query, $user_id) {
		global $mysqli;
		$all_categories = array();

		while ($row = $category_query->fetch_assoc()) {
			$category = buildCategoryArray($row, $user_id);
			
			array_push($all_categories, $category);
		}
		
		return json_encode($all_categories, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	function get_amount_categories($user_id) {
		global $mysqli;
		
		if ($user_id == -1)
			$amount_query = $mysqli->query("SELECT COUNT(*) FROM categories");
		else
			$amount_query = $mysqli->query("SELECT COUNT(*) FROM categories WHERE user_id=".$user_id);
			
		logIfError($mysqli, $user_id, "get_categories_db", "get_amount_categories", "1");	
		
		return $amount_query->fetch_assoc()['COUNT(*)'];
	}

	function get_all_categories_names($user_id) {
		global $mysqli;

		$categories_query = $mysqli->query("SELECT category_id, name FROM categories ORDER BY name ASC");

		logIfError($mysqli, $user_id, "get_categories_db", "get_all_categories_names", "1");


		$all_categories = array();

		while ($row = $categories_query->fetch_assoc()) {
			$category = array('category_id' => $row['category_id'], 'name' => $row['name']);
			
			array_push($all_categories, $category);
		}

		return json_encode($all_categories, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	if ( isset($_REQUEST['limit']) && isset($_REQUEST['user_id'])) {
		
		$activity="Get categories requested. Limit=".$_REQUEST['limit'];

		if ( isset($_REQUEST['from_id']) ) 
			$activity=$activity." fromId=".$_REQUEST['from_id'];

		if ( isset($_REQUEST['get_best']) ) 
			$activity=$activity." get_best=".$_REQUEST['get_best'];

		logMsg($_REQUEST['user_id'], $activity);

		$from_id = isset($_REQUEST['from_id']) ? $_REQUEST['from_id'] : -1;
		$from_score = isset($_REQUEST['from_score']) ? $_REQUEST['from_score'] : NULL_SCORE_VALUE;

		// Need to remove slashes always the request is via REST
		if (isset($_REQUEST['get_best'])) {
			echo get_bests_lasts_categories_db($_REQUEST['limit'], $from_id, $from_score, $_REQUEST['user_id'], $_REQUEST['get_best']);
		} else {
			echo get_user_category_db($_REQUEST['limit'], $from_id, $_REQUEST['user_id']);
		}
		
	}
	
?>
